﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using lgweb.common;
using LGCourt.Logic.BLL;
using LGCourt.Logic.NBL.DBEntities;
using KMFrame.Permission.Util;

namespace lgweb.inside
{
    public partial class ajaxLogin : UnNeedValidatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            string userName = this.GetQueryString("UserName").Trim();
            string password = this.GetQueryString("Password").Trim();
            string rtxserver = this.GetQueryString("ServerAddress").Trim();
            int rtxport = 0;
            int.TryParse(this.GetQueryString("ServerPort").ToString(),out rtxport);
            if (string.IsNullOrEmpty(userName))
            {
                ResponseResult(1, "请输入登录用户名！");
            }
            OA bll = new OA();

            vOAUser result = (vOAUser)bll.Login(userName, password);

            if (result != null)
            {
                UserSession user = new UserSession(result.userid, result.username, Guid.Empty);
                SessionBox.RemoveUserSession();
                SessionBox.CreateUserSession(user);
                string codeUserName=HttpUtility.UrlEncode(result.username);
                this.Session["UserName"] = codeUserName;
                this.Session["UserID"] = result.userid;
                Response.Cookies["CooikeUserName"].Value = codeUserName;
                Response.Cookies["CooikeUserName"].Expires = DateTime.Now.AddDays(1);
                string sSessionKey = GetSessionKey(result.username, rtxserver, rtxport);
                ResponseResult(result, sSessionKey);
                //Response.Redirect("/inside/index.aspx");
                return;
            }
            else
            {
                ResponseResult(2, "用户名或密码错误！");
                return;
            }
        }

        /// <summary>
        /// 单点登陆SessionKey
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        public string GetSessionKey(string pName, string pServerIP, int pServerPort)
        {
           // return RTX_Until.GetSessionKey(pName,pServerIP,pServerPort);
            return "";
        }


        private void ResponseResult(int code, string message)
        {
            string json = string.Format("{{ ResultCode:{0},Message:\"{1}\" }}", code.ToString(), message);
            Response.ContentType = "text/x-json";
            Response.Charset = "UTF-8";
            Response.Write(json);
            Response.End();
        }

        private void ResponseResult(vOAUser loginResult, string SessionKey)
        {
            string username = (loginResult.username);

            string json = string.Format("{{ ResultCode:{0},Message:\"{1}\" ,UserName:\"{2}\",SessionKey:\"{3}\" }}", "0", "", username, SessionKey);

            Response.ContentType = "text/x-json";
            Response.Charset = "UTF-8";
            Response.Write(json);
            Response.End();
        }
    }
}